import { _decorator, Component, director, EditBox, EventTouch, log, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('login_Scene')
export class login_Scene extends Component {
    onLoad() {
        // 监听微信登录按钮的点击事件
        this.node.on('click', this.onLoginBtnClicked, this);

    }

    start() {
        wx.cloud.init({
            env: 'cowgame-2gqtarpzd5ced304'
        });
    }

    openid
    //登录按钮的处理
    private onLoginBtnClicked(e: EventTouch) {
        var hcOpenId
        let self = this;
        //获取openId
        wx.cloud.callFunction({
            // 云函数名称
            name: 'mycloud',
            // 传给云函数的参数
            data: {},
            success: function (res) {
                hcOpenId = res.result.userInfo.openId;
                console.log('获取云函数mycloud的参数openId = ', hcOpenId);
                self.getOpenId();
            },
            fail: function (err) {
                console.error('获取云函数mycloud失败！', err);
            }
        });

        // 将openid存储到缓存中
        this.openid = hcOpenId;
    }


    getOpenId() {
        //查询数据库操作
        let db = wx.cloud.database();
        let userCollection = db.collection('user_info');
        //查询数据库是否有openid
        userCollection.where({
            _openid: this.openid
        }).get().then(res => {
            console.log('数据查询成功', res);

            // 数据库没有openid的数据 执行注册操作
            if (res.data.length === 0) {
                console.log('没有数据,注册');
                this.getUserProfile(userCollection);

            } else {
                console.log('有数据,登录');
                //登录之后把数据放到本地全局对象中
                this.setRes(res);
            }

        }).catch(err => {

            console.log('查询失败', err);
        });
    }

    // 获取用户资料,登录弹窗
    getUserProfile(userCollection) {
        // let openid = wx.getStorageSync('openid')
        let userInfo;
        let self = this;
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途
            success: (res) => {
                console.log('获取用户资料成功！：', res);
                let resPa = res;
                self.addLogin(userCollection,resPa)
                userInfo = res.userInfo;
            },
            fail: (err) => {
                console.error('获取用户资料失败！：', err);
                self.wxLogin(userCollection, userInfo);
            }
        });
        console.log('wx.getUserProfile的下面');
        
    }

    //自动注册
    addLogin(userCollection,resPa) {
        userCollection.add({
            data: {
                user_head_url: resPa.user_head_url,
                user_name: resPa.user_name,
                user_score: 0
            }
        }).then(res => {
            console.log('自动注册添加成功：',res);
            this.getOpenId();
        }).catch(err => {
            console.log('自动注册添加失败：', err);//失败提示错误信息
        });

    }

    //手动注册
    wxLogin(userCollection, e) {
        userCollection.add({
            data: {
                user_head_url: '123url.png',
                user_name: '小虎',
                user_score: 0
            }
        }).then(res => {
            //注册之后把数据放到本地全局对象中
            console.log('手动注册添加成功：',res);
            this.getOpenId();
        }).catch(err => {
            console.log('手动注册添加失败：', err);//失败提示错误信息
        });

    }


    setRes(res) {
        
        globalThis._userInfo.user_score = res.data[0].user_score;
        globalThis._userInfo.user_head_url = res.data[0].user_head_url;
        // globalThis._userInfo.user_id = res.data[0]._id;
        globalThis._userInfo.user_name = res.data[0].user_name;
        console.log('分数 = ', globalThis._userInfo.user_score);
        director.loadScene('scene3');
    }

}
